package personal.wang.manager;

import io.netty.channel.Channel;
import org.apache.curator.framework.CuratorFramework;
import personal.wang.domain.HeartbeatBean;
import personal.wang.message.Message;

import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class WorkerManager {
    private ConcurrentHashMap<HeartbeatBean.Endpoint, WorkerInfor> workers = new ConcurrentHashMap<>();

    public ConcurrentHashMap<HeartbeatBean.Endpoint, WorkerInfor> getWorkers() {
        return workers;
    }

    public void loadWorkerInfoFromZookeeper(CuratorFramework curatorFramework){

    }

    public void broadcastMessageToWorker(Message message){
        getWorkers().forEachValue(1 , workerInfor -> {
            workerInfor.getChannel().writeAndFlush(message);
        });
    }

    public String getWorkInfo() {
        return workers.keySet().stream().map(x -> x.getHost()).collect(Collectors.joining("\n"));
    }

    public static class WorkerInfor{
        private long timeToLive ;
        private io.netty.channel.Channel channel ;

        public long getTimeToLive() {
            return timeToLive;
        }

        public void setTimeToLive(long timeToLive) {
            this.timeToLive = timeToLive;
        }

        public Channel getChannel() {
            return channel;
        }

        public void setChannel(Channel channel) {
            this.channel = channel;
        }
        public WorkerInfor(long timeToLive, Channel channel) {
            this.timeToLive = timeToLive;
            this.channel = channel;
        }
    }
}
